﻿using System;
using System.Data;
using CRM.DAL.Ali;
using CRM.MODEL;
using CRM.UTIL;

namespace CRM.WEB
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btn_Login_Click(object sender, EventArgs e)
        {
            Session.Clear();//清空session
            System.Web.Security.FormsAuthentication.SignOut();//安全退出
            // LogHelper.WriteLog(typeof(Nullable), "时间:" + DateTime.Now + "</n>");
            string loginName = this.txt_username.Value.Trim();
            string password = this.txt_password.Value.Trim();
            if (string.IsNullOrEmpty(loginName))
            {
                MessageBox.Show(this.Page, "用户名不能为空!");
                return;
            }

            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show(this.Page, "密码不能为空!");
                return;
            }

            var obj =new CRM.BLL.Role.UsersB();
            var entity = new GetUsers
            {
                LOGINNAME_VALUE = loginName,
                PASSWORD_VALUE = Common.JiaMi(password),
                ISACTIVE_VALUE = 'Y'
            };
            DataTable dt=obj.GetUsers(ref entity);


            if (dt != null && dt.Rows.Count > 0)
            {
                string fullName = dt.Rows[0]["FullName"].ToString();
                string userID = dt.Rows[0]["ID"].ToString();
                SessionBox.CreateUserSession(new UserSession(userID, "", "", dt.Rows[0]["isAdmin"], "", "", fullName, "",
                    "", "", "",
                    loginName, dt.Rows[0]["Temp1"], dt.Rows[0]["password"], dt.Rows[0]["Temp2"]));
                string url = Request["ReturnUrl"];
                url = (string.IsNullOrEmpty(url)) ? "Default.aspx" : url;
                Response.Redirect(url);
            }
            else
            {
                var stu = new MODEL.Ali.GetAli_Supplier
                {
                    LOGINACCOUNTVALUE = loginName,
                    PASSWORDVALUE = Common.JiaMi(password),
                    ISACTIVEVALUE = "Y"
                };
                var serv = new BLL.Ali.Ali_SupplierB();
                var sdt = serv.GetAli_Supplier(ref stu);
                if (sdt != null && sdt.Rows.Count > 0)
                {
                    string name = sdt.Rows[0]["SupplierName"].ToString();
                    string id = sdt.Rows[0]["ID"].ToString();
                    string pid = sdt.Rows[0]["PID"].ToString();
                    SessionBox.CreateUserSession(new UserSession(id, string.Empty, string.Empty, string.Empty,
                        string.Empty, string.Empty, name, string.Empty,
                        string.Empty, string.Empty, string.Empty,
                        loginName, pid == "0" ? id : pid, sdt.Rows[0]["PassWord"], pid == "0" ? string.Empty : pid));
                    string url = Request["ReturnUrl"];
                    url = (string.IsNullOrEmpty(url)) ? "Default.aspx" : url;
                    Response.Redirect(url);
                }
                MessageBox.Show(this.Page, "用户或者密码错误!");
            }
        }
    }
}